package com.yauchu.kegra.graph.traversers;

import java.util.ArrayList;
import java.util.Iterator;

import org.jgrapht.DirectedGraph;

import android.view.Gravity;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.yauchu.kegra.graph.GraphNode;
import com.yauchu.kegra.graph.NumberedEdge;
import com.yauchu.kegra.oelib.R;

public class GraphViewGenerator extends GraphNodeTraverser<LinearLayout> {

	// private LinearLayout mainView;
	// private int depth;
	// private GraphNode rootNode;

	public GraphViewGenerator(DirectedGraph<GraphNode, NumberedEdge> graph) {
		super(graph);
		// this.mainView=mainView;
		// this.rootNode=rootNode;
		// this.depth=depth;

	}

	protected LinearLayout reduce(GraphNode current, ArrayList<LinearLayout> res) {

		if (current.displayLayout != null) {
			
			RelativeLayout relative= new RelativeLayout(current.displayLayout.getContext());
			TextView dotText= new TextView(current.displayLayout.getContext());
			dotText.setText(".");
			
			RelativeLayout.LayoutParams lp= new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,RelativeLayout.LayoutParams.WRAP_CONTENT);
			dotText.setId(1);
			current.imageView.setId(2);
			relative.addView(current.imageView);
			lp.addRule(RelativeLayout.BELOW,current.imageView.getId());
			lp.addRule(RelativeLayout.CENTER_IN_PARENT);
			relative.addView(dotText,lp);
			lp= new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,RelativeLayout.LayoutParams.WRAP_CONTENT);
			lp.addRule(RelativeLayout.BELOW,dotText.getId());
			lp.addRule(RelativeLayout.CENTER_IN_PARENT);
			Iterator<LinearLayout> viewIter = res.iterator();
			relative.addView(viewIter.next(),lp);
			relative.addView(viewIter.next(),lp);
			
			//LinearLayout newLayout = new LinearLayout(current.displayLayout.getContext());
			//newLayout.setOrientation(LinearLayout.HORIZONTAL);
			//newLayout.setGravity(Gravity.CENTER);
			//newLayout.setShowDividers(LinearLayout.SHOW_DIVIDER_MIDDLE);
			
			//newLayout.setBackgroundDrawable(current.displayLayout.getContext().getResources().getDrawable(R.drawable.background));

			//LinearLayout newVerticalLayout = new LinearLayout(current.displayLayout.getContext());
			//newVerticalLayout.setOrientation(LinearLayout.VERTICAL);
			//newVerticalLayout.setGravity(Gravity.CENTER);

			//Iterator<LinearLayout> viewIter = res.iterator();

			/*
			if (viewIter.hasNext()) {
				newVerticalLayout.addView(current.displayLayout,LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.MATCH_PARENT);
				newVerticalLayout.addView(newLayout,LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.MATCH_PARENT);

				while (viewIter.hasNext()) {
					
					newLayout.addView(viewIter.next(),LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.MATCH_PARENT);
				}
			} else {
				newVerticalLayout.addView(current.displayLayout,LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.MATCH_PARENT);
			}

			return newVerticalLayout;
			*/
		} else {
			return null;
		}

	}
      
	public LinearLayout generateView(GraphNode rootNode, int depth) {
		return this.reduceInOrderDownToDepth(rootNode, depth);

	}

}
